package cn.ehcache.impl;


import cn.ehcache.abstracts.AbstractCacheManagerProvider;
import cn.ehcache.abstracts.SimpleCacheManager;
import lombok.Getter;
import org.ehcache.Cache;

import java.time.Duration;


/**
 * 类简介: 令牌缓存提供程序
 *
 * @Author: ShangGuan
 * @Time: 2024/05/13
 **/
@Getter
public class TokenCacheProviderImpl extends AbstractCacheManagerProvider<String, String> {

    /**
     * 过期时间
     */
    private final Duration duration = Duration.ofMinutes(118);


    /**
     * 开辟内存
     */
    private final Integer capacity = 10000;

    private final Cache<String, String> stringCache;

    public TokenCacheProviderImpl() {
        super(String.class, String.class, TokenCacheProviderImpl.class.getName(), SimpleCacheManager.instants());
        Cache<String, String> superCache = getCache();
        if (superCache != null) {
            stringCache = getCache();
        } else {
            stringCache = createCache(duration, capacity);
        }
    }


    public String getClassName() {
        return this.getClass().getName();
    }


    @Override
    public void put(String key, String value) {
        stringCache.put(key, value);
    }

    @Override
    public String get(String key) {
        return stringCache.get(key);
    }

    @Override
    public void remove(String key) {
        stringCache.remove(key);
    }

    @Override
    public void clear() {
        stringCache.clear();
    }

    @Override
    public boolean hasKey(String key) {
        return stringCache.containsKey(key);
    }


}
